#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StdTcp.h"
#include "StdThread.h"

struct Message
{
    int flag;
    char fromname[20];
    char toname[20];
    char content[1024];
};
typedef struct Message Msg;
void *thread_headle(void *arg)
{
    TcpC *c=(TcpC *)arg;
    while(1)
    {
        Msg m;
        TcpClinetRecv(c,&m,sizeof(m));
        printf("recv message from %s ,message %s\n",m.fromname,m.content);
    }
}
void main(int argc,char *argv[])
{

    if(argc!=3)
    {
        printf("invalid numbers\n");
        return;
    }
    TcpC *c=InitTcpClinet(argv[1],atoi(argv[2]));
    Msg m;
    scanf("%s",m.fromname);
    while(getchar()!='\n');
    m.flag=1;
    TcpClinetSend(c,&m,sizeof(m));
    m.flag=2;
    thread *t=InitThread(thread_headle,c);
    while(1)
    {
        printf("please input send name:\n");
        scanf("%s",m.toname);
        while(getchar()!='\n');
        printf("please input send content:\n");
        scanf("%[^\n]",m.content);
        while(getchar()!='\n');
        TcpClinetSend(c,&m,sizeof(m));
    }
}